#!/usr/bin/env python
# encoding: utf-8
# @author: qiaowei
# @license: (C) Copyright 2013-2019
# @contact: weiweiqiao@126.com
# @file: main_frame_module.py
# @time: 2020/6/13
# @desc:

from PySide2.QtWidgets import QMainWindow
from PySide2.QtWidgets import QAction
from PySide2.QtCore import Slot
from PySide2.QtCore import QRegExp
from PySide2.QtGui import QRegExpValidator
from PySide2.QtCore import QThread


from forms.FrequencyFrame import Ui_MainWindow
from py_package.file_manager_module import FileManager
from py_package.table_model_module import TableModel


class MainFrame(QMainWindow):
    def __init__(self, parent=None):
        """
        初始化窗体

        Args:
            parent: 初始化参数

        """
        super().__init__(parent)
        # 初始化父类
        # QMainWindow.__init__(self, parent)
        # super(MainFrame, self).__init__()

        # 打印当前线程
        # print(QThread.currentThread())

        self.file_manager = FileManager()
        # self.centralWidget().setFixedSize(self.sizeHint())

        # Pyside2中的窗体初始化类似Qt中的步骤，将MainFrame控件传递给Ui_MainWindow的setupUi方法，在
        # setupUi方法内将Ui_MainWindow的控件传给MainFrame
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        # 创建控件并添加到MainFrame窗体
        self.__init_widgets()

        # 初始化信号槽
        self.__init_connect()

        self.table_model = TableModel()
        self.ui.tableview_station_information.setModel(self.table_model)
        # self.ui.tableview_station_information.setModel(self.__table_model)

    def __init_connect(self):
        """
        创建控件的SIGNAL/SLOT

        """
        self.action_open.triggered.connect(self.open_file)
        self.action_exit.triggered.connect(self.exit_application)
        self.ui.button_test_frequency.clicked.connect(self.test_frequency)

    @Slot()
    def open_file(self):
        """
        通过文件框选择文件并读取数据，将数据返回

        """
        information_tuple = self.file_manager.read_file()

        if information_tuple[1] is not None:
            self.ui.label_station_name.setText(information_tuple[0])
            self.ui.tableview_station_information.setModel(information_tuple[1])
        elif information_tuple[0] is not None:
            self.ui.label_station_name.setText(information_tuple[0] + " 文件内容为空")

    @Slot()
    def exit_application(self):
        """
        调用file_manager对象方法，退出程序

        """
        self.file_manager.exit_application()

    def test_frequency(self):
        self.file_manager.calculate_frequency(float(self.ui.lineedit_frequency.text()))

    def __init_widgets(self):
        """
        向窗体添加QAction，菜单栏等控件

        """
        self.action_open = QAction("打开文档")
        self.action_exit = QAction("退出程序")

        # 添加菜单栏
        self.menu_file = self.menuBar().addMenu("文档")
        self.menu_file.addAction(self.action_open)
        self.menu_file.addAction(self.action_exit)

        # 输入的测试频率的只能是浮点型数据
        self.ui.lineedit_frequency.setValidator(QRegExpValidator(
            QRegExp("[1-9]\d*\.\d*|0\.\d*[1-9]\d*$")))

